package com.shop.loan.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.shop.loan.mapper.LoanOrderBillByMybatisMapper;
import com.shop.loan.mapper.LoanOrderByMybatisMapper;
import com.shop.loan.mapper.UserByMybatisMapper;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.entity.LoanOrderBillByMybatis;
import com.shop.loan.model.entity.LoanOrderByMybatis;
import com.shop.loan.model.enums.OrderStatus;
import com.shop.loan.rentaldata.RentalDataUtil;
import com.shop.loan.rentaldata.RentalPayUtil;
import com.shop.loan.service.StatisticsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    LoanOrderByMybatisMapper loanOrderByMybatisMapper;

    @Resource
    UserByMybatisMapper userByMybatisMapper;

    @Resource
    LoanOrderBillByMybatisMapper loanOrderBillByMybatisMapper;

    @Resource
    RentalDataUtil rentalDataUtil;

    @Resource
    RentalPayUtil RentalPayUtil;

    @Override
    public ResResult num() {
        JSONObject jsonObject = new JSONObject();

        Date startTime = DateUtil.beginOfDay(new Date());
        Date endTime = DateUtil.endOfDay(new Date());

        //总用户数
        Integer totalUser = userByMybatisMapper.selectCount(null, null);
        //今日注册人数
        Integer todayUser = userByMybatisMapper.selectCount(startTime.toString(), endTime.toString());

        LoanOrderByMybatis loanOrderByMybatis = new LoanOrderByMybatis();
        //总订单数
        Integer totalOrder = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
        //新客订单数
        loanOrderByMybatis.setOrderNum(1);
        Integer totalOrderByNew = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
        //老客订单数
        loanOrderByMybatis.setOrderNum(2);
        Integer totalOrderByOld = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
        loanOrderByMybatis.setOrderNum(null);
        //驳回订单数
        loanOrderByMybatis.setStatus(OrderStatus.refuse.getStatus());
        Integer refuseOrder = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
        refuseOrder = refuseOrder == null ? 0 : refuseOrder;
        //放款驳回订单数
        loanOrderByMybatis.setStatus(OrderStatus.refuse_loan.getStatus());
        Integer loanRefuseOrder = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
        loanRefuseOrder = loanRefuseOrder == null ? 0 : loanRefuseOrder;
        refuseOrder = refuseOrder + loanRefuseOrder;

        //进行中订单数

        loanOrderByMybatis.setStatus(OrderStatus.underway.getStatus());
        Integer ingOrder = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
        loanOrderByMybatis.setOrderNum(1);
        Integer ingOrderByNew = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);

        loanOrderByMybatis.setOrderNum(2);
        Integer ingOrderByOld = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
        loanOrderByMybatis.setOrderNum(null);
        //已结束
        loanOrderByMybatis.setStatus(OrderStatus.end.getStatus());
        Integer endOrder = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);


        loanOrderByMybatis.setStatus(null);
        loanOrderByMybatis.setStartTime(startTime.toString());
        loanOrderByMybatis.setEndTime(endTime.toString());
        Integer todayOrder = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
        //今日驳回订单数
        loanOrderByMybatis.setStatus(OrderStatus.refuse.getStatus());
        Integer todayRefuseOrder = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
        todayRefuseOrder = todayRefuseOrder == null ? 0 : todayRefuseOrder;
        //放款驳回
        loanOrderByMybatis.setStatus(OrderStatus.refuse_loan.getStatus());
        Integer todayLoanRefuseOrder = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);
        todayLoanRefuseOrder = todayLoanRefuseOrder == null ? 0 : todayLoanRefuseOrder;
        todayRefuseOrder = todayRefuseOrder + todayLoanRefuseOrder;
        //今日待审核订单数
        loanOrderByMybatis.setStatus(OrderStatus.wait_auth.getStatus());
        Integer todayWaitAuthOrder = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);

        //今日放款订单数
        loanOrderByMybatis.setStartTime(null);
        loanOrderByMybatis.setEndTime(null);
        loanOrderByMybatis.setStartTimeByLoan(startTime.toString());
        loanOrderByMybatis.setEndTimeByLoan(endTime.toString());
        loanOrderByMybatis.setStatus(-1);
        Integer todayIngOrder = loanOrderByMybatisMapper.selectCount(loanOrderByMybatis);

        loanOrderByMybatis.setStartTimeByLoan(null);
        loanOrderByMybatis.setEndTimeByLoan(null);
        loanOrderByMybatis.setStatus(null);
        loanOrderByMybatis.setStartTime(startTime.toString());
        loanOrderByMybatis.setEndTime(endTime.toString());
        LoanOrderBillByMybatis billSum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
        if (billSum == null || billSum.getMoney() == null) {
            billSum = new LoanOrderBillByMybatis();
            billSum.setMoney(BigDecimal.ZERO);
            billSum.setDiscountsMoney(BigDecimal.ZERO);
            billSum.setPayMoney(BigDecimal.ZERO);
            billSum.setRateMoney(BigDecimal.ZERO);

        }
        //今日应还款金额
        BigDecimal todayPeriodMoney = billSum.getMoney().add(billSum.getRateMoney());
        //今日应还款本息
        BigDecimal todayPeriodPrincipal = billSum.getMoney().add(billSum.getRateMoney());
        //今日应还款担保费
//        BigDecimal todayPeriodGuarantee = billSum.getShopMoney();
        //今日已还本息
        BigDecimal todayPeriodPayMoney = billSum.getPayMoney();
        //今日已还商品金额
        BigDecimal todayPeriodPayMoneyByGuarantee = BigDecimal.ZERO;

        BigDecimal todayPaySum = todayPeriodPayMoney.add(todayPeriodPayMoneyByGuarantee);


        //还款率/利润率
        BigDecimal todayPayRate = todayPaySum.divide(todayPeriodMoney.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : todayPeriodMoney, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));
        BigDecimal todayProfitRate = (todayPaySum.subtract(billSum.getMoney())).divide(todayPeriodPrincipal.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : todayPeriodPrincipal, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));


        loanOrderByMybatis.setStartTime(DateUtil.offsetDay(startTime, 1).toString());
        loanOrderByMybatis.setEndTime(DateUtil.offsetDay(endTime, 1).toString());
        billSum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
        if (billSum == null || billSum.getMoney() == null) {
            billSum = new LoanOrderBillByMybatis();
            billSum.setMoney(BigDecimal.ZERO);
            billSum.setDiscountsMoney(BigDecimal.ZERO);
            billSum.setPayMoney(BigDecimal.ZERO);
            billSum.setRateMoney(BigDecimal.ZERO);
        }
        //明日应还款金额
        BigDecimal tomPeriodMoney = billSum.getMoney().add(billSum.getRateMoney());
        //明日应还款本息
        BigDecimal tomPeriodPrincipal = billSum.getMoney().add(billSum.getRateMoney());
        //明日应还款担保费
//        BigDecimal tomPeriodGuarantee = billSum.getShopMoney();
        //明日已还本息
        BigDecimal tomPeriodPayMoney = billSum.getPayMoney();
        //明日已还商品金额
        BigDecimal tomPeriodPayMoneyByGuarantee = BigDecimal.ZERO;

        BigDecimal tomPaySum = tomPeriodPayMoney.add(tomPeriodPayMoneyByGuarantee);

        BigDecimal tomPayRate = tomPaySum.divide(tomPeriodMoney.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : tomPeriodMoney, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));
        BigDecimal tomProfitRate = (tomPaySum.subtract(billSum.getMoney())).divide(tomPeriodPrincipal.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : tomPeriodPrincipal, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));


        loanOrderByMybatis.setStartTime(null);
        loanOrderByMybatis.setEndTime(null);

        billSum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
        if (billSum == null || billSum.getMoney() == null) {
            billSum = new LoanOrderBillByMybatis();
            billSum.setMoney(BigDecimal.ZERO);
            billSum.setDiscountsMoney(BigDecimal.ZERO);
            billSum.setPayMoney(BigDecimal.ZERO);
            billSum.setRateMoney(BigDecimal.ZERO);
        }
        //总应还款金额
        BigDecimal totalPeriodMoney = billSum.getMoney().add(billSum.getRateMoney());
        //应还款本息
        BigDecimal totalPeriodPrincipal = billSum.getMoney().add(billSum.getRateMoney());
        //应还款担保费
//        BigDecimal totalPeriodGuarantee = billSum.getShopMoney();
        //已还本息
        BigDecimal totalPeriodPayMoney = billSum.getPayMoney();
        //已还商品金额
        BigDecimal totalPeriodPayMoneyByGuarantee = BigDecimal.ZERO;
        //优惠本息
        BigDecimal discountsMoney = billSum.getDiscountsMoney();
        //优惠商品金额
        BigDecimal discountsMoneyByGoods = BigDecimal.ZERO;

        BigDecimal totalPaySum = totalPeriodPayMoney.add(totalPeriodPayMoneyByGuarantee);

        BigDecimal totalPayRate = totalPaySum.divide(totalPeriodMoney.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : totalPeriodMoney, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));
        BigDecimal totalProfitRate = (totalPaySum.subtract(billSum.getMoney())).divide(totalPeriodPrincipal.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : totalPeriodPrincipal, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));


        //总逾期金额
        loanOrderByMybatis.setStatus(OrderStatus.underway.getStatus());
        loanOrderByMybatis.setOverdueStatus(false);
        billSum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
        if (billSum == null || billSum.getMoney() == null) {
            billSum = new LoanOrderBillByMybatis();
            billSum.setMoney(BigDecimal.ZERO);
            billSum.setDiscountsMoney(BigDecimal.ZERO);
            billSum.setPayMoney(BigDecimal.ZERO);
            billSum.setRateMoney(BigDecimal.ZERO);
        }
        BigDecimal totalMoneyOverdue = billSum.getMoney().add(billSum.getRateMoney()).subtract(billSum.getPayMoney());

        //今日逾期金额(昨天到期的账单)
        loanOrderByMybatis.setStartTime(DateUtil.offsetDay(startTime, -1).toString());
        loanOrderByMybatis.setEndTime(DateUtil.offsetDay(endTime, -1).toString());
        loanOrderByMybatis.setStatus(OrderStatus.underway.getStatus());
        loanOrderByMybatis.setOverdueStatus(false);
        billSum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
        if (billSum == null || billSum.getMoney() == null) {
            billSum = new LoanOrderBillByMybatis();
            billSum.setMoney(BigDecimal.ZERO);
            billSum.setDiscountsMoney(BigDecimal.ZERO);
            billSum.setPayMoney(BigDecimal.ZERO);
            billSum.setRateMoney(BigDecimal.ZERO);
        }
        BigDecimal todayMoneyOverdue = billSum.getMoney().add(billSum.getRateMoney()).subtract(billSum.getPayMoney());

        //数据余额
        BigDecimal dataBalance = rentalDataUtil.getBalance();
        //小非凡余额
        JSONObject payBalanceJs = RentalPayUtil.getBalance();

        jsonObject.put("dataBalance", dataBalance == null ? 0 : dataBalance);
        jsonObject.put("assetBalance", payBalanceJs.getBigDecimal("assetBalance"));
        jsonObject.put("loanBalance", payBalanceJs.getBigDecimal("loanBalance"));
        jsonObject.put("rdBalance", payBalanceJs.getBigDecimal("rdBalance"));

        jsonObject.put("totalUser", totalUser == null ? 0 : totalUser);
        jsonObject.put("totalOrder", totalOrder == null ? 0 : totalOrder);
        jsonObject.put("totalOrderByNew", totalOrderByNew == null ? 0 : totalOrderByNew);
        jsonObject.put("totalOrderByOld", totalOrderByOld == null ? 0 : totalOrderByOld);
        jsonObject.put("refuseOrder", refuseOrder);
        jsonObject.put("ingOrder", ingOrder == null ? 0 : ingOrder);
        jsonObject.put("ingOrderByNew", ingOrderByNew == null ? 0 : ingOrderByNew);
        jsonObject.put("ingOrderByOld", ingOrderByOld == null ? 0 : ingOrderByOld);
        jsonObject.put("endOrder", endOrder == null ? 0 : endOrder);
        jsonObject.put("todayUser", todayUser == null ? 0 : todayUser);
        jsonObject.put("todayOrder", todayOrder == null ? 0 : todayOrder);
        jsonObject.put("todayWaitAuthOrder", todayWaitAuthOrder == null ? 0 : todayWaitAuthOrder);
        jsonObject.put("todayIngOrder", todayIngOrder == null ? 0 : todayIngOrder);
        jsonObject.put("todayRefuseOrder", todayRefuseOrder == null ? 0 : todayRefuseOrder);
        jsonObject.put("todayPayRate", todayPayRate == null ? 0 : todayPayRate);
        jsonObject.put("todayProfitRate", todayProfitRate == null ? 0 : todayProfitRate);

        jsonObject.put("totalProfitRate", totalProfitRate == null ? 0 : totalProfitRate);
        jsonObject.put("totalPayRate", totalPayRate == null ? 0 : totalPayRate);

        jsonObject.put("tomPayRate", tomPayRate == null ? 0 : tomPayRate);
        jsonObject.put("tomProfitRate", tomProfitRate == null ? 0 : tomProfitRate);

        jsonObject.put("todayPeriodPrincipal", todayPeriodPrincipal == null ? 0 : todayPeriodPrincipal);
//        jsonObject.put("todayPeriodGuarantee", todayPeriodGuarantee == null ? 0 : todayPeriodGuarantee);
        jsonObject.put("todayPeriodMoney", todayPeriodMoney == null ? 0 : todayPeriodMoney);
        jsonObject.put("todayPeriodPayMoney", todayPeriodPayMoney == null ? 0 : todayPeriodPayMoney);
        jsonObject.put("todayPeriodPayMoneyByGuarantee", todayPeriodPayMoneyByGuarantee == null ? 0 : todayPeriodPayMoneyByGuarantee);

        jsonObject.put("tomPeriodPrincipal", tomPeriodPrincipal == null ? 0 : tomPeriodPrincipal);
//        jsonObject.put("tomPeriodGuarantee", tomPeriodGuarantee == null ? 0 : tomPeriodGuarantee);
        jsonObject.put("tomPeriodMoney", tomPeriodMoney == null ? 0 : tomPeriodMoney);
        jsonObject.put("tomPeriodPayMoney", tomPeriodPayMoney == null ? 0 : tomPeriodPayMoney);
        jsonObject.put("tomPeriodPayMoneyByGuarantee", tomPeriodPayMoneyByGuarantee == null ? 0 : tomPeriodPayMoneyByGuarantee);

        jsonObject.put("todayMoneyOverdue", todayMoneyOverdue == null ? 0 : todayMoneyOverdue);
        jsonObject.put("totalMoneyOverdue", totalMoneyOverdue == null ? 0 : totalMoneyOverdue);

        jsonObject.put("totalPeriodMoney", totalPeriodMoney == null ? 0 : totalPeriodMoney);
        jsonObject.put("totalPeriodPrincipal", totalPeriodPrincipal == null ? 0 : totalPeriodPrincipal);
        jsonObject.put("totalPeriodPayMoney", totalPeriodPayMoney == null ? 0 : totalPeriodPayMoney);
//        jsonObject.put("totalPeriodGuarantee", totalPeriodGuarantee == null ? 0 : totalPeriodGuarantee);
        jsonObject.put("totalPeriodPayMoneyByGuarantee", totalPeriodPayMoneyByGuarantee == null ? 0 : totalPeriodPayMoneyByGuarantee);
        jsonObject.put("discountsMoney", discountsMoney == null ? 0 : discountsMoney);
        jsonObject.put("discountsMoneyByGoods", discountsMoneyByGoods == null ? 0 : discountsMoneyByGoods);
        return new ResResult(Code.SUCCESS, jsonObject);
    }


}
